package com.shengsheng.expschedule.service.expschedule.controller.admin;


import com.shengsheng.expschedule.base.result.R;
import com.shengsheng.expschedule.base.util.JwtInfo;
import com.shengsheng.expschedule.base.util.JwtUtils;
import com.shengsheng.expschedule.service.expschedule.entity.Schedule;
import com.shengsheng.expschedule.service.expschedule.entity.Vo.ScheduleQueryVo;
import com.shengsheng.expschedule.service.expschedule.service.ScheduleService;
import com.shengsheng.expschedule.service.expschedule.util.TokenProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 实验室选课表 前端控制器
 * </p>
 *
 * @author yuan
 * @since 2021-09-27
 */
@RestController
@RequestMapping("/admin/expschedule/schedule")
@Api(tags = "排课管理")
@RefreshScope
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private TokenProperties tokenProperties;


    @ApiOperation("新增排课")
    @PostMapping("/save")
    public R save(@ApiParam(value = "排课对象", required = true) @RequestBody Schedule schedule) {

        if (scheduleService.save(schedule)) return R.ok().message("添加成功");
        return R.error().message("添加失败");
    }

    @ApiOperation("根据排课查询对象获取排课")
    @PostMapping("/getSchedule")
    public R getByWeekNum(@ApiParam(value = "排课查询对象", required = true)
                          @RequestBody ScheduleQueryVo scheduleQueryVo) {

        Map<String, List> schedule = scheduleService.getScheduleByScheduleQueryVo(scheduleQueryVo);
        return R.ok()
                .data("schedule", schedule)
                .message("获取数据成功");
    }

    @ApiOperation("删除排课信息")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "排课 Id", required = true) @PathVariable String id) {

        if (scheduleService.removeById(id)) return R.ok().message("删除成功");
        return R.error().message("数据不存在");
    }

    @ApiOperation("修改排课信息")
    @PutMapping("/update")
    public R update(@ApiParam(value = "排课对象", required = true) @RequestBody Schedule schedule) {

        if (scheduleService.updateById(schedule)) return R.ok().message("更新成功");
        return R.error().message("数据不存在");
    }

    @ApiOperation("根据排课 Id 查询排课信息")
    @GetMapping("/getById/{id}")
    public R getById(@ApiParam(value = "排课 Id", required = true) @PathVariable String id) {

        Schedule schedule = scheduleService.getById(id);
        if (schedule == null) return R.error().message("数据不存在");
        return R.ok()
                .data("schedule", schedule)
                .message("获取排课信息成功");
    }

    @ApiOperation("生成添加网站链接")
    @GetMapping("/generatorLinks")
    public R generatorLinks() {
        String token = JwtUtils.getJwtToken(new JwtInfo(),tokenProperties.getExpireTime());
        return R.ok()
                .data("token", token)
                .message("生成成功");
    }
}

